<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>
        // #### 实现一个函数createCounter，接受一个初始值作为参数，返回一个新的函数，每次调用该函数返回一个递增的数字，初始值为createCounter函数的参数，但是当调用该函数时传入一个参数，会将计数器的值重置为传入的参数。

        const createCounter = function (e) {
            let counter = e
            const fn = function (b) {
                if (typeof b !== 'number') {
                    return ++counter
                } else {
                    return counter = ++b
                }

            }
            return fn
        }



        //实现以下效果
        const counter = createCounter(5);
        console.log(counter()); // 6
        console.log(counter()); // 7
        console.log(counter()); // 8
        console.log(counter(10)); // 11
        console.log(counter('')); // 12

    </script>
</body>

</html>